package com.marchsoft.service.impl;

import com.marchsoft.common.enums.ExceptionEnum;
import com.marchsoft.common.exception.CommonException;
import com.marchsoft.common.status.ActivityStatu;
import com.marchsoft.common.utils.TimeUtils;
import com.marchsoft.dao.ActivityMapper;
import com.marchsoft.dao.StudentMapper;
import com.marchsoft.domain.Activity;
import com.marchsoft.domain.Student;
import com.marchsoft.service.IStudentService;
import com.marchsoft.vo.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentServiceImpl implements IStudentService {

    @Resource
    private StudentMapper studentMapper;

    @Resource
    private ActivityMapper activityMapper;

    @Override
    public List<Student> findAll() {
        return studentMapper.selectAll();
    }

    @Override
    public StudentExtra queryStudentInfo(Integer accountId) {
        return studentMapper.queryStudentInfo(accountId);
    }

    /**
     * 查询学生信息（附带专业导师信息、附带所属兴趣小组的信息、附带兴趣小组的老师信息）
     * @param sid 学生id
     * @return 学生信息（附带专业导师信息、附带所属兴趣小组的信息、附带兴趣小组的老师信息）
     */
    @Override
    public StudentExtra queryStudentInfoWithTeacher(Integer sid){
        if (studentMapper.queryStudentInfoWithTeacher(sid)==null){
            throw new CommonException(ExceptionEnum.STUDENT_NOT_FOUND);
        }
        return studentMapper.queryStudentInfoWithTeacher(sid);
    }

    /**
     * 学生退组更改其状态
     * @param sid 学生id
     * @param gid 小组id
     * @return 返回true&false
     */
    @Override
    @Transactional(rollbackFor=Exception.class)
    public Boolean updateGroupWithStudentBySid(Integer sid, Integer gid) {
        Student stu=new Student();
        stu.setSid(sid);
        //将学生表的groupId改为0，标识退组状态
        stu.setGroupId(ActivityStatu.STUDENT_EXIT_GROUP_GROUPID);
        //更新学生表的小组id为0
        int count1=studentMapper.updateByPrimaryKeySelective(stu);

        Activity act=new Activity();
        act.setSid(sid);
        act.setGroupId(gid);
        //设置学生的小组状态为退组
        act.setStatus(ActivityStatu.STUDENT_EXIT_GROUP);
        //获取系统当前时间
        act.setATime(new Timestamp(System.currentTimeMillis()));
        int count2=activityMapper.insertSelective(act);
        if (count1==1&&count2==1){
            return true;
        }
        return false;
    }

    /**
     * 处理查询的历史导师的信息
     * @param sid 学生id
     * @return 处理后历史导师的信息的集合
     */
    @Override
    public List<HistoryTeacherExtra> dealHistoryTeacherInfo(Integer sid) {
        //拿到最初的历史导师的集合(根据学生id和状态查询)
        List<HistoryTeacherExtra> historyTeacherInfoList=activityMapper.queryHistoryTeacherBySidWithStatus(sid);
        List<HistoryTeacherExtra> dealHistoryTeacherInfoList=new ArrayList<>();
        for (int i=0;i<historyTeacherInfoList.size();i=i+2){
            historyTeacherInfoList.get(i).setTeacherStartTime(historyTeacherInfoList.get(i).getInterimTime());
            historyTeacherInfoList.get(i).setTeacherEndTime(historyTeacherInfoList.get(i+1).getInterimTime());
            historyTeacherInfoList.get(i).setInterimTime(null);
            dealHistoryTeacherInfoList.add(historyTeacherInfoList.get(i));
        }
        return dealHistoryTeacherInfoList;
    }

    /**
     * 处理查询的历史小组的信息
     * @param sid 学生id
     * @return 处理后历史小组的信息的集合
     */
    @Override
    public List<HistoryGroupExtra> dealHistoryGroupInfo(Integer sid) {
        //拿到最初的历史小组信息的集合(根据学生id和状态查询)
        List<HistoryGroupExtra> historyGroupInfoList=activityMapper.queryHistoryGroupBySidWithStatus(sid);
        List<HistoryGroupExtra> dealHistoryGroupInfoList=new ArrayList<>();
        for (int i=0;i<historyGroupInfoList.size();i=i+2){
            historyGroupInfoList.get(i).setGroupStartTime(historyGroupInfoList.get(i).getInterimTime());
            historyGroupInfoList.get(i).setGroupEndTime(historyGroupInfoList.get(i+1).getInterimTime());
            historyGroupInfoList.get(i).setInterimTime(null);
            dealHistoryGroupInfoList.add(historyGroupInfoList.get(i));
        }
        return dealHistoryGroupInfoList;
    }

    /**
     * 查询学生信息和历史导师、历史兴趣小组信息
     * @param sid 学生id
     * @return 学生信息和历史导师、历史兴趣小组信息
     */
    @Override
    public Map<String,Object> studentWithHistoryInfo(Integer sid){
        Map<String,Object> studentWithHistoryMap=new HashMap<>();
        studentWithHistoryMap.put("studentInfo",queryStudentInfoWithTeacher(sid));
        studentWithHistoryMap.put("historyTeacher",dealHistoryTeacherInfo(sid));
        studentWithHistoryMap.put("historyGroup",dealHistoryGroupInfo(sid));
        return studentWithHistoryMap;
    }

    @Override
    public StuAndHistoryTea queryStuAndHistoryTea(Integer sid) {
        StuExtra student  = studentMapper.queryStudentByStuId(sid);
        StuAndHistoryTea stuAndHistoryTea = new StuAndHistoryTea();
        stuAndHistoryTea.setStudent(student);
        List<HistoryTeacherExtra> list = activityMapper.queryHistoryTeacherBySidWithStatus(sid);
        List<StuAndHistoryTea.HistoryTeacher> teachers = new ArrayList<>();
        for (int i = 0; i < list.size(); i += 2) {
            StuAndHistoryTea.HistoryTeacher tea = new StuAndHistoryTea.HistoryTeacher();
            tea.setName(list.get(i).getTeacherHistoryName());
            tea.setStartTime(TimeUtils.getNewTime(list.get(i).getInterimTime()));
            if (i + 1 < list.size()) {
                tea.setEndTime(TimeUtils.getNewTime(list.get(i + 1).getInterimTime()));
            }
            teachers.add(tea);
        }
        stuAndHistoryTea.setTeachers(teachers);

        return stuAndHistoryTea;
    }
    @Override
    public List<Student> queryMystudentByGroup(Integer gid) {
        Student stu = new Student();
        stu.setGroupId(gid);
        List<Student> list = studentMapper.select(stu);
        return list;
    }

    @Override
    public List<Student> queryMystudentByteacher(Integer tid) {
        Student stu = new Student();
        stu.setTid(tid);
        List<Student> list = studentMapper.select(stu);
        return list;
    }


}
